﻿using CKS.Core.Framework;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP130
{
    /// <summary>
    /// 拆胶塞
    /// </summary>
    public class DB3011_Work01Action : PLCAction
    {
        public DB3011_Work01Action(DecodeValue _value) : base(_value)
        {
        }
        public override void Execute()
        {
            var result = Value.Value.ToInt();
            var replayDB = 3010;
            var replayAction = 10;
            if (result == 1)
            {

                DcOp130Record dc = new DcOp130Record()
                {
                    CreatedTime = DateTime.Now,
                    ProductCode = Value.GetLinkData(1).ToString(),
                    PreTestingResult1 = Value.GetLinkData(2).ToInt(),
                    PostTestingResult2 = Value.GetLinkData(3).ToInt(),
                };

                

                CatchAction(() => {
                    db.Insertable(dc).ExecuteCommand();
                }, "OP130请求保存数据");
                
                var replayPos = Value.GetPosition(replayDB, replayAction);
                AckWriteInt16(replayPos, 1);
                EventBus.AddLog($"OP130请求保存数据{dc.ProductCode}");
            }
            else
            {
                var replayPos = Value.GetPosition(replayDB, replayAction);
                AckWriteInt16(replayPos, 0);
                EventBus.AddLog("OP130 重置保存状态");
            }
        }
    }
}
